package com.googlecode.gwtbb.client.only.widget.display.forum;

import com.google.gwt.user.client.ui.*;
import com.googlecode.gwtbb.client.only.GwtUtils;
import com.googlecode.gwtbb.client.only.widget.display.user.UserLink;
import com.googlecode.gwtbb.client.shared.entity.forum.Post;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 16, 2008
 */
public class PostDetail extends Composite {
	private DockPanel mainPanel;
	private Post post;

	public PostDetail(Post post) {
		DecoratorPanel decorator = new DecoratorPanel();
		initWidget(decorator);
		decorator.setStyleName("gwtbb-PostDetail-decorator");
		mainPanel = new DockPanel();
		decorator.add(mainPanel);
		mainPanel.setWidth("100%");
		mainPanel.setStylePrimaryName("gwtbb-PostDetail");
		setPost(post);
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
		mainPanel.clear();
		if (post != null) {
			DockPanel topBar = new DockPanel();
			mainPanel.add(topBar, DockPanel.NORTH);
			topBar.setStylePrimaryName("gwtbb-PostDetail-topBar");
			topBar.setWidth("100%");
			Post.Info postInfo = post.getInfo();
			if (postInfo != null) {
				topBar.add(new UserLink(postInfo.getUser()), DockPanel.WEST);
			}
			Label dateLabel = new Label(GwtUtils.formatSec(post.getCreateDate()));
			topBar.add(dateLabel, DockPanel.EAST);
			topBar.setCellHorizontalAlignment(dateLabel, HasHorizontalAlignment.ALIGN_RIGHT);

			Label body = new Label(post.getBody());
			mainPanel.add(body, DockPanel.CENTER);
			body.setStylePrimaryName("gwtbb-PostDetail-body");
		}
	}
}
